<?php

namespace App\Models\OpenShop\Common;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Relations\HasOne;

class MallSettleInShopInfoModel extends BaseModel
{
    protected $table = 'mall_settle_in_shop_info';

    protected $fillable = [
        'mallId',
        'mallName',
        'province',
        'city',
        'address',
        'lng',
        'lat',
        'storeId',
        'storeName',
        'branchStore',
        'firstType',
        'secondType',
        'floor',
        'businessCircle',
        'price',
        'score',
        'star',
        'phone',
        'commentCount',
        'status',
        'statusText',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];


    /**
     * 通过mallId获得所有的楼层
     * @param string $mallId
     * @return array
     */
    public static function getEachFloorNameByMallId(string $mallId): array
    {
        $list = [];
        self::query()
            ->where('mallId', $mallId)
            ->groupBy('floor')
            ->pluck('floor')
            ->filter()
            ->each(function ($item) use (&$list){
                $list[$item] = $item;
            });
        $list['金街'] = '金街';
        return $list;
    }


    /**
     * 通过mallId和楼层获得所有的店铺
     * @param string $mallId
     * @param string $floor
     * @return array
     */
    public static function getStoreByMallIdANDFloor(string $mallId, string $floor): array
    {
        return self::query()
            ->where('mallId', $mallId)
            ->where('floor', $floor)
            ->select('storeName')
            ->get()
            ->map(function ($item) {
                return $item->storeName;
            })->toArray();
    }


    /**
     * @return HasOne
     */
    public function siteLocationMallRel(): HasOne
    {
        return $this->hasOne(SiteLocationMallModel::class, 'mallId', 'mallId');
    }

}
